/** @file    FormUserManager.cpp
 *  @author  Alessandro Polo
 *  @version $Id: FormUserManager.cpp 2867 2010-08-07 20:44:51Z alex $
 *  @brief
 * File containing methods for the wosh::gui::FormUserManager class.
 * The header for this class can be found in FormUserManager.h, check that file
 * for class description.
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

 #include "FormUserManager.h"
 #include "ui_FormUserManager.h"
 #include <core/UserManager.h>

 #include <framework/gui/widgets/wCore_User.h>


using namespace std;
namespace wosh {
 namespace gui {

 WOSH_REGISTER(wosh::gui::FormUserManager, "wosh::gui::IWidgetGeneric", 1.0, _static_FormUserManager )
 WOSH_GUI_MAP(wosh::gui::FormUserManager, "wosh::UserManager", 0.0, _static_Map_FormUserManager )

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

FormUserManager::FormUserManager(QWidget *parent)
	: IWidgetGeneric(parent) {
	this->m_ui = new Ui::FormUserManager();
	this->m_ui->setupUi(this);

	IWidgetGeneric::setTargetURI(_UserManager_URI);
	buildMenus();

	// tab Users [0]
	this->m_ui->tableViewUsers->setResizeStretch(5);
	this->m_ui->tableViewUsers->getTableView()->setContextMenuPolicy(Qt::CustomContextMenu);
	connect(this->m_ui->tableViewUsers, SIGNAL(postMessage(Message*)),this, SLOT(postMessage(Message*)) );
	connect(this->m_ui->tableViewUsers, SIGNAL(tableContextMenuRequested(const QPoint&, const QModelIndex&)),this, SLOT(tableViewUsers_ContextMenu(const QPoint&, const QModelIndex&)) );
	connect(this->m_ui->tableViewUsers->getTableView(), SIGNAL(doubleClicked(const QModelIndex&)), this, SLOT(tableViewUsers_doubleClicked(const QModelIndex&)));

	// tab Groups [1]
	this->m_ui->tableViewGroups->setMethodList(_UserManager_METHOD_listgroups);
	this->m_ui->tableViewGroups->setResizeStretch(1);
	this->m_ui->tableViewGroups->getTableView()->setContextMenuPolicy(Qt::CustomContextMenu);
	connect(this->m_ui->tableViewGroups, SIGNAL(postMessage(Message*)),this, SLOT(postMessage(Message*)) );
	connect(this->m_ui->tableViewGroups, SIGNAL(tableContextMenuRequested(const QPoint&, const QModelIndex&)),this, SLOT(tableViewGroups_ContextMenu(const QPoint&, const QModelIndex&)) );
	connect(this->m_ui->tableViewGroups->getTableView(), SIGNAL(doubleClicked(const QModelIndex&)), this, SLOT(tableViewGroups_doubleClicked(const QModelIndex&)));

	// tab Lookup [2]
	connect(this->m_ui->btnLookupByProperty, SIGNAL(clicked()), this, SLOT(btnLookupByProperty_clicked()));
	this->m_ui->tableViewLookup->setMethodList(_UserManager_METHOD_search);
	this->m_ui->tableViewLookup->setResizeStretch(1);
	connect(this->m_ui->tableViewLookup->getUpdateBar(), SIGNAL(updateClicked()), this, SLOT(clearLookupResults()));

 }

FormUserManager::~FormUserManager() {
	delete this->m_ui;
 }

void FormUserManager::changeEvent(QEvent *e) {
	QWidget::changeEvent(e);
	if ( e->type() == QEvent::LanguageChange )
		this->m_ui->retranslateUi(this);
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

void FormUserManager::refreshAll() {
	this->setWindowTitle( QString("User Manager [%1]").arg(this->getTargetURI().getKernelName().c_str()) );
	this->m_ui->tableViewUsers->refreshData();
	this->m_ui->tableViewGroups->refreshData();
 }

void FormUserManager::clearLookupResults() {
	this->m_ui->tableViewLookup->getTableView()->loadSerializable(NULL);
	this->m_ui->tableViewLookup->getUpdateBar()->sayReady();
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

void FormUserManager::tableViewUsers_doubleClicked( const QModelIndex& ) {
	Variant dataName = this->m_ui->tableViewUsers->getCurrentItemAsVariantAtCol(1);
	wCore_User* user = new wCore_User();
	URI userURI;
	userURI.setParent( this->getTargetURI() );
	userURI.setName( dataName.toString() );
	user->setTargetURI( userURI );
	user->refreshAll();
	user->show();
 }

void FormUserManager::tableViewGroups_doubleClicked( const QModelIndex& ) {
/*
	if ( this->wTableModelGroups->getDataTable() == NULL ) return;
	const ISerializable* objID = this->wTableModelGroups->getDataTable()->get(index.row()+(unsigned int)this->wTableModelGroups->getDataTable()->hasHeader(), 0);
	if ( objID == NULL ) return;
	if ( !objID->isKindOf<Variant>() ) return;
	const Variant* dataID = dynamic_cast<const Variant*>(objID);
*/
 }

void FormUserManager::btnLookupByProperty_clicked() {
	Property* ppattern = new Property();
	ppattern->setKey(this->m_ui->txtLookupPropertyKey->text().toLatin1().data());
	if ( !this->m_ui->txtLookupPropertyValue->getData().isEmpty() && this->m_ui->txtLookupPropertyValue->getData().isStringNotEmpty() )
		ppattern->setValue(this->m_ui->txtLookupPropertyValue->getData());
	executeRemoteMethod( _UserManager_METHOD_search, ppattern );
	this->m_ui->tableViewLookup->getUpdateBar()->sayRetrieving();
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

void FormUserManager::buildMenus() {
	this->menuUserTitle = new QAction(this);
	this->menuUserTitle->setEnabled(false);
	this->menuUserSendMessage = new QAction(this);
	this->menuUserSendMessage->setText(tr("&Send Message"));
	this->menuUser = new QMenu(this);
	this->menuUser->addAction(this->menuUserTitle);
	this->menuUser->addSeparator();
	this->menuUser->addAction(this->menuUserSendMessage);

	this->menuGroupTitle = new QAction(this);
	this->menuGroupTitle->setEnabled(false);
	this->menuGroupInquire = new QAction(this);
	this->menuGroupInquire->setText(tr("&Inquire"));
	this->menuGroup = new QMenu(this);
	this->menuGroup->addAction(this->menuGroupTitle);
	this->menuGroup->addSeparator();
	this->menuGroup->addAction(this->menuGroupInquire);
 }

void FormUserManager::tableViewUsers_ContextMenu( const QPoint& pos, const QModelIndex& index ) {
	QPoint globalPos = this->mapToGlobal(pos);
	if ( !index.isValid() || index.row() == 0 ) return;
	Variant userName = this->m_ui->tableViewUsers->getCurrentItemAsVariantAtCol(1);

	this->menuUserTitle->setText( userName.toString().c_str() );
	this->menuUserTitle->setIcon( GuiUtilities::createIconFor("wosh::User") );

	QAction* selected = this->menuUser->exec( globalPos );
	if ( selected == this->menuUserSendMessage ) {
	 }
 }

void FormUserManager::tableViewGroups_ContextMenu( const QPoint& pos, const QModelIndex& index ) {
	QPoint globalPos = this->mapToGlobal(pos);
	if ( !index.isValid() || index.row() == 0 ) return;
	Variant userName = this->m_ui->tableViewGroups->getCurrentItemAsVariantAtCol(1);

	this->menuGroupTitle->setText( userName.toString().c_str() );
	this->menuGroupTitle->setIcon( GuiUtilities::createIconFor("wosh::User") );

	QAction* selected = this->menuGroup->exec( globalPos );
	if ( selected == this->menuGroupInquire ) {
	 }
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

void FormUserManager::busMessage( const Message& message, const Bus* ) {
	if ( message.isEmpty() || !message.getContent()->isResponse() ) return;
	if ( !isMessageForMeOrFromTarget(message) ) return;
	const wosh::Response* response = message.getContent()->asResponse();

	this->m_ui->tableViewUsers->evalResponse(response);
	this->m_ui->tableViewGroups->evalResponse(response);
	this->m_ui->tableViewLookup->evalResponse(response);

	promptMethodResponse(message);
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

 }; // namespace gui
}; // namespace wosh
